﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PiaoanWeb.AppLogic.BusinessLogicLayer;
using PiaoanWeb.AppLogic.DataAccessLayer;
using PiaoanWeb.Helpers;
namespace PiaoanWeb.Admin
{
    public partial class EditProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProductData();
            }
        }

        private void BindProductData()
        {
            ProductsBLL productsBLL = new ProductsBLL();
            Products products = productsBLL.GetProductByID(Convert.ToInt32(Request.QueryString["ProductID"]));
            nameTxtBox.Text = products.Name;
            descriptionTxtbox.Text = WebUtility.HtmlDecode(products.Description);
            CategoriesBLL categoryBLL = new CategoriesBLL();
            categoryDDL.DataSource = categoryBLL.GetAllCategoriesIncludeDeleted();
            categoryDDL.DataBind();
            categoryDDL.SelectedValue = products.CategoryID.ToString();
            ProductImage.ImageUrl = "~/ProductPictures/" + products.ProductImage;
        }
        
        protected void uploadBtn_Click(object sender, EventArgs e)
        {
            if (ProductImageUpload.HasFile)
            {
                ProductsBLL productsBLL = new ProductsBLL();
                Products products = productsBLL.GetProductByID(Convert.ToInt32(Request.QueryString["ProductID"]));
                string imageFileName = Guid.NewGuid() + Path.GetExtension(ProductImageUpload.FileName);
                ProductImageUpload.SaveAs(Server.MapPath("~/ProductPictures/" + imageFileName));
                products.ProductImage = imageFileName;
                products.Name = nameTxtBox.Text;
                products.Description = descriptionTxtbox.Text;
                products.CategoryID = Convert.ToInt32(categoryDDL.SelectedValue);
                productsBLL.Save();

                BindProductData();
            }
        }

        protected void saveBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ProductsBLL productsBLL = new ProductsBLL();
                Products products = productsBLL.GetProductByID(Convert.ToInt32(Request.QueryString["ProductID"]));
                string imageFileName = Guid.NewGuid() + Path.GetExtension(ProductImageUpload.FileName);
                if (ProductImageUpload.HasFile)
                {
                    ProductImageUpload.SaveAs(Server.MapPath("~/ProductPictures/" + imageFileName));
                    products.ProductImage = imageFileName;
                }
                products.Name = nameTxtBox.Text;
                products.Description = descriptionTxtbox.Text;
                products.CategoryID = Convert.ToInt32(categoryDDL.SelectedValue);
                productsBLL.Save();

                BindProductData();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }   
        }

        protected void cancelBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductList.aspx");
        }
    }
}